﻿Imports System.Windows.Forms
Imports System.Text.RegularExpressions

Public Class FormSeparation

    Public Modele As String = "$0"

    Private Sub AjusteModeles()
        Me.ComboBoxModele.Items.Clear()
        Me.ComboBoxModele.Text = ""
        Me.RichTextBox1.Text = ""
        If Me.RadioButtonMarque.Checked Then
            For Each oItem As ListViewItem In Form1.ListViewMarqueurs.CheckedItems
                Me.ComboBoxModele.Items.Add(oItem.Text)
            Next
            If Me.ComboBoxModele.Items.Count > 0 Then
                Me.ComboBoxModele.Text = ""
                Me.ComboBoxModele.Text = Me.ComboBoxModele.Items(0)
            End If

        ElseIf Me.RadioButtonRegEx.Checked Then
            Me.ComboBoxModele.Items.Add("Expression régulière")
            Me.RichTextBox1.Text = Me.Modele
            Me.ComboBoxModele.Text = "Expression régulière"
        End If
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If Me.TextBox1.Text = "" Then
            MsgBox("Vous devez obligatoirement définir un modèle de nom de fichier", MsgBoxStyle.Exclamation)
        ElseIf Me.RichTextBox1.Text = "" Then
            MsgBox("Vous devez obligatoirement définir un modèle de contenu", MsgBoxStyle.Exclamation)
        Else
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub ButtonImporter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonImporter.Click
        Me.OpenFileDialog1.Multiselect = False
        Me.OpenFileDialog1.Filter = "Tous|*.*"
        If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Try
                Me.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(Me.OpenFileDialog1.FileName, System.Text.Encoding.Default)
            Catch Ex As Exception
                MsgBox("Erreur à la lecture du fichier " & Me.OpenFileDialog1.FileName & ": " & Ex.Message, MsgBoxStyle.Exclamation)
            End Try
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FormBrowser.WebBrowser1.DocumentText = My.Resources.SeparationHelp
        FormBrowser.Show()
    End Sub

    Private Sub ButtonPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPreview.Click
        'Preparer la collection des documents à traiter
        Dim ColDocs As New Collection
        If Me.RadioButtonCourant.Checked Then
            ColDocs.Add(Form1.DocumentCourant)
        Else
            For Each oItem As ListViewItem In Form1.ListViewFichiers.Items
                ColDocs.Add(oItem.Tag)
            Next
        End If
        'Préparer la collection des expressions régulières a utiliser
        Dim ColMarqueurs As New Collection
        If Me.RadioButtonMarque.Checked Then
            'Récupérer les marqueurs actifs
            For Each oItem As ListViewItem In Form1.ListViewMarqueurs.CheckedItems
                 ColMarqueurs.Add(oItem.Tag)
            Next
        ElseIf Me.RadioButtonRegEx.Checked Then
             Dim oMarqueur As New ClassMarqueur
            oMarqueur.sMasque = Me.TextBoxRegex.Text
            oMarqueur.Modele = Me.Modele
            ColMarqueurs.Add(oMarqueur)
        End If
        'Boucler sur tous les documents 
        Dim iIndex As Long = 0
        Dim sPreview As String = ""
        For Each oDoc As ClassDocument In ColDocs
            'On traite toutes les expressions
            For Each oMarqueur As ClassMarqueur In ColMarqueurs
                Dim oOption As RegexOptions = RegexOptions.Multiline
                If oMarqueur.SensibleCasse = Boolean.FalseString Then oOption = oOption Or RegexOptions.IgnoreCase
                Dim oRegEx As New Regex(oMarqueur.Masque, oOption)
                For Each oMatch As Match In oRegEx.Matches(oDoc.Contenu)
                    iIndex += 1
                    Dim sContenu As String = oMarqueur.Modele
                    Dim sNom As String = Me.TextBox1.Text
                    'On s'assure que le modele contient un élément unique
                    If Not Regex.IsMatch(sNom, "\$No|\$\d+") Then sNom = sNom.Replace(".", "_" & iIndex.ToString("0") & ".")
                    sContenu = Regex.Replace(sContenu, "\$No", iIndex.ToString("0"), RegexOptions.IgnoreCase)
                    sContenu = Regex.Replace(sContenu, "\$Nom", oDoc.Titre, RegexOptions.IgnoreCase)
                    sContenu = Regex.Replace(sContenu, "\$Chemin", oDoc.Chemin, RegexOptions.IgnoreCase)
                    sContenu = Regex.Replace(sContenu, "\$Dossier", My.Computer.FileSystem.GetParentPath(oDoc.Chemin), RegexOptions.IgnoreCase)
                    sNom = Regex.Replace(sNom, "\$No", iIndex.ToString("0"), RegexOptions.IgnoreCase)
                    sNom = Regex.Replace(sNom, "\$Nom", oDoc.Titre, RegexOptions.IgnoreCase)
                    sNom = Regex.Replace(sNom, "\$Chemin", oDoc.Chemin, RegexOptions.IgnoreCase)
                    sNom = Regex.Replace(sNom, "\$Dossier", My.Computer.FileSystem.GetParentPath(oDoc.Chemin), RegexOptions.IgnoreCase)
                    For iSubMatch As Integer = 0 To oMatch.Groups.Count - 1
                        sContenu = sContenu.Replace("$" & iSubMatch.ToString("0"), oMatch.Groups(iSubMatch).Value)
                        sNom = sNom.Replace("$" & iSubMatch.ToString("0"), oMatch.Groups(iSubMatch).Value)
                    Next
                    If sPreview <> "" Then sPreview &= vbCrLf
                    sPreview &= sNom & vbCrLf & Indent(sContenu, 4) & vbCrLf
                Next 'Match
            Next 'Regex
        Next 'Doc
        FormPreview.Text = "Extraction/Séparation"
        FormPreview.RichTextBox1.Text = sPreview
        FormPreview.ShowDialog()
    End Sub

    Private Sub RadioButtonRegEx_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonRegEx.CheckedChanged
        Me.TextBoxRegex.Enabled = Me.RadioButtonRegEx.Checked
        Call AjusteModeles()
    End Sub

    Private Sub ComboBoxModele_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxModele.SelectedIndexChanged
        If Me.RadioButtonRegEx.Checked Then
            Me.RichTextBox1.Text = Me.Modele
        ElseIf Me.RadioButtonMarque.Checked Then
            If Form1.ListViewMarqueurs.Items.ContainsKey(Me.ComboBoxModele.Text) Then
                Dim oMarqueur As ClassMarqueur = Form1.ListViewMarqueurs.Items(Me.ComboBoxModele.Text).Tag
                Me.RichTextBox1.Text = oMarqueur.Modele
            End If
        End If
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If Me.RadioButtonRegEx.Checked Then
            Me.Modele = Me.RichTextBox1.Text
        ElseIf Me.RadioButtonMarque.Checked Then
            If Form1.ListViewMarqueurs.Items.ContainsKey(Me.ComboBoxModele.Text) Then
                Dim oMarqueur As ClassMarqueur = Form1.ListViewMarqueurs.Items(Me.ComboBoxModele.Text).Tag
                oMarqueur.Modele = Me.RichTextBox1.Text
            End If
        End If
    End Sub

    Private Sub RadioButtonMarque_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonMarque.CheckedChanged
        Call AjusteModeles()
    End Sub

    Private Sub FormSeparation_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        If Me.RadioButtonRegEx.Checked Then
            Me.RichTextBox1.Text = Me.Modele
        ElseIf Me.RadioButtonMarque.Checked Then
            If Form1.ListViewMarqueurs.Items.ContainsKey(Me.ComboBoxModele.Text) Then
                Dim oMarqueur As ClassMarqueur = Form1.ListViewMarqueurs.Items(Me.ComboBoxModele.Text).Tag
                Me.RichTextBox1.Text = oMarqueur.Modele
            End If
        End If

    End Sub

    Private Sub Button12_Click(sender As System.Object, e As System.EventArgs) Handles Button12.Click
        FormRegEx.TextBoxRegEx.Text = Me.TextBoxRegex.Text
        FormRegEx.RichTextBoxTest.Text = ""
        If FormRegEx.ShowDialog = DialogResult.OK Then
            Me.TextBoxRegex.Text = FormRegEx.TextBoxRegEx.Text
        End If
    End Sub
End Class
